# 6.9 Extension Method Extension
The Extension Method is used to add extension methods to existing classes externally. This feature is similar to the "mixin" feature in the Ruby language.
By default, Enjoy has added 9 extension methods (toBigDecimal()
, toBigInteger()
, toInt()
, toLong()
, toFloat()
, toDouble()
, toBoolean()
, toShort()
, toByte()
) for 9 commonly used Java types: BigDecimal
, BigInteger
, String
, Integer
, Long
, Float
, Double
, Short
, and Byte
. Below is an example of how to use them:
#set(age = "18")
#if(age.toInt() >= 18)
join to the party
#end
2
3
4
In the first line of the example above, age
is of type String
. Since String
has been extended with the toInt()
method, it can be called to compare it with the Integer
value 18.
Note: BigDecimal
and BigInteger
were added in version 5.1.2. You will need to upgrade to this version or extend it yourself.
Extension Method has two main application scenarios. The first is to extend the functionality of existing classes in the Java library. Below is an example of adding extension methods to the Integer
class:
public class MyIntegerExt {
public Integer square(Integer self) {
return self * self;
}
public Double power(Integer self, Double exponent) {
return Math.pow(self, exponent);
}
public Boolean isOdd(Integer self) {
return self % 2 != 0;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
As shown in the code above, the first parameter of the three extension methods must be of type Integer
, so that this parameter can hold the caller itself when the method is called. The remaining parameters can be of any type, as demonstrated by the exponent
parameter in the power
method. Extension methods must have at least one parameter. The following code configures these extension methods:
Engine.addExtensionMethod(Integer.class, MyIntegerExt.class);
After this simple two-step process, you can use the extension methods in the template:
#set(num = 123)
#(num.square())
#(num.power(10D))
#(num.isOdd())
2
3
4
The second, third, and fourth lines in the example above call the three extension methods in MyIntegerExt
, implementing the square, the 10th power, and odd number check for 123.
As shown in the examples above, extension methods allow you to extend the functionality of classes outside of themselves, making their usage in templates very convenient.
The second important application scenario for Extension Method is to make uncertain types certain. For example, the Controller.keepPara()
method treats all parameters as String
types. So, an Integer
type from a form will turn into a String
after keepPara()
, triggering a type mismatch exception in the template. Here's how to resolve this:
<select name="type">
#for(x : list)
<option value="#(x.type)" #if(x.type == type.toInt()) selected #end />
#end
</select>
2
3
4
5
Assuming type
is of Integer
type, the select
field will turn type
into a String
after submitting the form and using keepPara()
on the backend. To re-render the template without causing a type mismatch, use type.toInt()
. Of course, you can also resolve this by using keepPara(Integer.class, “type”)
.